package org.fuys.etp.core;

import lombok.extern.slf4j.Slf4j;
import org.fuys.etp.core.model.event.EtpAlertEvent;
import org.fuys.etp.core.model.event.EtpCheckEvent;
import org.fuys.etp.core.model.event.EtpPersistentConfigChangeEvent;
import org.fuys.etp.core.model.event.EtpReportEvent;
import org.fuys.etp.core.model.res.TpConfigUpdateRes;
import org.fuys.etp.core.model.res.TpStatusResult;
import org.fuys.etp.external.alert.IAlertService;
import org.fuys.etp.external.alert.model.req.AlertReq;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core
 * @className: EtpDispatcher
 * @author: WangJing
 * @description: 弹性线程池调度者
 * @date: 2025/2/10 21:14
 * @version: 1.0
 */
@Component
@Slf4j
public class EtpDispatcher {

    @Resource
    private EtpManager etpManager;

    @Resource
    private EtpCollector etpCollector;

    Map<String, IAlertService> alertServiceMap;

    public EtpDispatcher(ListableBeanFactory beanFactory){
        alertServiceMap = beanFactory.getBeansOfType(IAlertService.class);
        if(ObjectUtils.isEmpty(alertServiceMap)){
            alertServiceMap = new HashMap<>();
        }
    }

    @Component
    public class EtpReportReceiver implements ApplicationListener<EtpReportEvent>{
        @Override
        public void onApplicationEvent(EtpReportEvent event) {
            log.debug("Fuys etp:接收到线程池报告事件:{}",event);
            final TpStatusResult report = event.getReport();
            if(report == null){
                return;
            }
            TpConfigUpdateRes tpUpdateResult = etpManager.updateByCheck(report.getPoolName(),report.getPoolType(),
                    event);
            if(tpUpdateResult.isSuccess()){
                //如果配置更新成功 则说明配置被更改 更新报告
                report.setConfigVO(tpUpdateResult.getCurConfig());
            }
            //更新监控数据
            etpCollector.updateForMetrics(report);
        }

    }

    @Component
    public class EtpAlertReceiver implements ApplicationListener<EtpAlertEvent>{

        @Override
        public void onApplicationEvent(EtpAlertEvent event) {
            log.debug("Fuys etp:接收到线程池告警事件:{}",event);
            final String alertType = event.getAlertType();
            final IAlertService iAlertService = alertServiceMap.get(alertType);
            if(iAlertService == null){
                log.error("Fuys etp:没有指定的报警器:{}", alertType);
                return;
            }
            AlertReq req = AlertReq.builder().curStatus(event.getStatus())
                    .alertTo(event.getStatus().getStrategyVO().getAlertTo())
                    .desc("线程池告警").subject("线程池告警").build();
            iAlertService.alert(req);
        }
    }


    @Component
    public class EtpSelfCheckReceiver implements ApplicationListener<EtpCheckEvent>{

        @Override
        public void onApplicationEvent(EtpCheckEvent event) {
            log.debug("Fuys etp:接收到线程池自检事件:{}",event);
            etpManager.updateByCheck(event.getPoolName(),event.getPoolType(),
                    event.getReportEvent());
        }

    }

    @Component
    public class EtpPersistentConfigChangeReceiver implements ApplicationListener<EtpPersistentConfigChangeEvent>{

        @Override
        public void onApplicationEvent(EtpPersistentConfigChangeEvent event) {
            log.info("Fuys etp:接收到线程池历史记录持久化配置变更事件:{}",event);
            etpManager.updatePersistentConfig(event.getConfig());
        }
    }
}
